<div class="layui-fluid">
    <div class="layui-breadcrumb jy-breadcrumb">

    </div>
    <div class="layui-card">
    <div class="layui-card-header">
        <span style="float:right;padding: 0px 60px 0px 0px">
            <a class="layui-btn layui-btn-normal layui-btn-sm " href="/#/Datamanufacture/manufacture" >返回</a>
            <button type="button" class="layui-btn layui-btn-normal layui-btn-sm batchUpdate" >SPU批量修改</button>
            <button type="button" class="layui-btn layui-btn-normal layui-btn-sm oneUpdate"  >单个保存</button>
        </span>
    </div>
    <div class="layui-row" >
        <div class="layui-col-md8 b">
            <div class="layui-tab" lay-filter="showInfo" style="background-color:#FFFFFF">
                <ul class="layui-tab-title">
                    <li class="layui-this" lay-id="1">标题</li>
                    <li lay-id="2">描述</li>
                    <li lay-id="3">产品属性</li>
                    <li lay-id="4">多属性</li>
                </ul>
                <input type="hidden" id='tabtype' value='1'>
                <div class="layui-tab-content" style="background-color:#FFFFFF">
                    <div class="layui-tab-item layui-show">
                        <div class="layui-card">
                            <div class="layui-form LAY-list-form-template pd15" lay-filter="LAY-list-form-template">
                                <div id="titleValueId" style="margin: 4px 0;display: none"></div>
                                <div id="titleValueLengthId"  style="margin: 4px 0;display: none">字数</div>
                                <div class="layui-form-item">
                                        <label class="layui-form-label layui-form-label-yh">分类</label>
                                        <div class="layui-input-block">
                                            <select name="name"   lay-filter="template_name">
                                                <option value="">请选择</option>
                                            </select>
                                        </div>
                                </div>
                                <div class=" LAY-list-form-template-content">

                                </div>
                                <button  class="layui-btn none" lay-submit lay-filter="LAY-list-form-template-Submit">保存</button>
                            </div>
                        </div>
                    </div>

                    <div class="layui-tab-item">
                        <div class="layui-form">
                        <div class="layui-card desc">
                        </div>
                            <button  class="layui-btn none" lay-submit lay-filter="LAY-list-form-desc-Submit">保存</button>
                        </div>
                    </div>


                    <div class="layui-tab-item">
                        <div class="layui-card">

                                <div class="layui-form-item" >
                                    <label class="layui-form-label layui-form-label-yh">速卖通分类</label>
                                    <div class="layui-input-block">
                                        <cascader id="cateSmt"></cascader>
                                    </div>
                                </div>
                                <div class="layui-form pd15 " lay-filter="LAY-list-form-att">
                                    <div class="LAY-list-form-att">

                                    </div>
                                    <button  class="layui-btn none" lay-submit lay-filter="LAY-list-form-att-Submit">保存</button>
                                </div>
                            <div class="layui-form  none" lay-filter="LAY-list-form-att-customize" >

                                <div class="layui-form-item">
                                        <label class="layui-form-label layui-form-label-yh">自定义属性</label>
                                        <div class="layui-input-block">
                                            <button class="layui-btn layui-btn-primary">添加新属性</button>
                                        </div>
                                </div>
                                <div class="layui-form-item">
                                    <input type="text"  placeholder="属性名" name="zdy1[]" class="layui-input zInput1"> :
                                    <input type="text"  placeholder="属性值" name="zdy2[]" class="layui-input zInput2">
                                </div>

                                <div class="layui-form-item">
                                    <input type="text"  placeholder="属性名" name="zdy1[]" class="layui-input zInput1"> :
                                    <input type="text"  placeholder="属性值" name="zdy2[]" class="layui-input zInput2">
                                </div>

                            </div>
                        </div>
                    </div>
                    <div class="layui-tab-item">     
                        <div class="layui-card">
                            <table id="LAY-list-normal"  lay-filter="LAY-list-normal"></table>
                        </div>   
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-col-md4 b skuInfo" style="position: fixed; right: 2%; background-color: #fff; width: 360px;">

        </div>
    </div>
    </div>
</div>
<style>
    .zInput1{
        display: inline-block;
        width: 94px;
        margin-left: 68px;
    }
    .zInput2{
        width: 200px;
        display: inline-block;
        margin-left: 20px;
    }
    #weight:after {
        content: 'g';
        position: absolute;
        top: 0px;
        right: 10px;
    }
    .category_id .xm-select-title{
        width: 500px;
    }
    .layui-layedit-tool{
        display: none;
    }
    [data-edit="text"] .layui-table-cell:before{
        font-family:layui-icon!important;font-size:16px;font-style:normal;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;
        content: "\e642";
        float: right;
    }
    .desc .layui-form-label-required{
        width: 300px;
    }
    .layui-form-select {
        width: 200px;
    }
    .layui-tab-content .layui-form-label-required,.layui-tab-content .layui-form-label-yh{
        width: 160px;
    }
    .layui-tab-content .layui-form-label-required +.layui-input-block,.layui-tab-content .layui-form-label-yh +.layui-input-block {
        margin-left: 190px;
    }
    .desc .layui-card-header{
        width: 100%;
    }
    .weight:after{
        content: 'g';
        position: absolute;
        top: 0px;
        right: 10px;
    }
</style>
<script type="text/html" id="skuInfo">
    {{# if(d.new){ }}
    <div class="layui-row grid-demo grid-demo-bg1">
        <div class="layui-col-md12" style="">
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">SKU</label>
                <div class="layui-inline">{{d.sku||''}}</div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">中文名称</label>
                <div class="layui-inline">{{d.name||''}}</div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">自带包装尺寸</label>
                <div class="layui-inline">{{d.aslength||0}}*{{d.aswide||0}}*{{d.ashight||0}}mm</div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">自带包装重量</label>
                <div class="layui-inline">{{d.asweight||0}}g</div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">打包后重量</label>
                <div class="layui-inline">{{d.baleweight||0}}g</div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">容量/体积</label>
                <div class="layui-inline">{{d.bulk||''}}{{d.bulk_unit||''}}</div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">品牌Logo</label>
                <div class="layui-inline">{{d.logo||''}}</div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">是否授权</label>
                <div class="layui-inline">{{d.authorization==1?'已授权':'未授权'}}</div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">描述等级</label>
                <div class="layui-inline">{{d.grade||''}}</div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">开发人员</label>
                <div class="layui-inline">{{d.developer||''}}</div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">仿品(品牌)</label>
                <div class="layui-inline">{{d.imitation||''}}</div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">盗图/实拍图</label>
                <div class="layui-inline">{{d.realpic||''}}</div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">调研链接</label>
                <div class="layui-inline"><a class="blue" href="{{d.research_links||''}}" target="_blank">点击跳转</a></div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">开发链接</label>
                <div class="layui-inline"><a class="blue" href="{{d.develop_link||''}}" target="_blank">点击跳转</a></div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">交接备注</label>
                <div class="layui-inline">{{d.remarks||''}}</div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">审核备注</label>
                <div class="layui-inline">{{d.audit_remarks||''}}</div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh" style="width: 120px">销售数量/单位</label>
                <div class="layui-inline">{{d.quantity||''}}{{d.quantity_unit||''}}</div>
            </div>
        </div>
    </div>
    {{# }else{ }}

    <div class="layui-row grid-demo grid-demo-bg1">
        <div class="layui-col-md12" style="">
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">SKU</label>
                <div class="layui-inline">{{d.sku||''}}</div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">中文名称</label>
                <div class="layui-inline">{{d.name||''}}</div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh" style="line-height: 38px">自带包装尺寸</label>
                <div class="layui-inline" >
                    <input type="text" name="lwh"   value="{{d.aslength||0}}*{{d.aswide||0}}*{{d.ashight||0}}" placeholder="格式:0*0*0" autocomplete="off" class="layui-input">
                    </div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh" style="line-height: 38px">打包后尺寸</label>
                <div class="layui-inline" >
                    <input type="text" name="balelwh"   value="{{d.balelength||0}}*{{d.balewide||0}}*{{d.balehight||0}}" placeholder="格式:0*0*0" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-item">
                <label class="layui-label layui-form-label-yh" style="line-height: 38px">自带包装重量</label>
                <div class="layui-inline weight" style="line-height: 38px">
                    <input type="text" name="asweight"   value="{{d.asweight||''}}" placeholder="" autocomplete="off" class="layui-input ">
                </div>
            </div>

            <div class="layui-item">
                <label class="layui-label layui-form-label-yh" style="line-height: 38px">打包后重量</label>
                <div class="layui-inline weight" style="line-height: 38px">
                    <input type="text" name="baleweight"   value="{{d.baleweight||''}}" placeholder="" autocomplete="off" class="layui-input ">
                </div>
            </div>

            <div class="layui-item">
                <label class="layui-label layui-form-label-yh" style="line-height: 38px">容量/体积</label>
                <div class="layui-inline " style="line-height: 38px">
                    <input type="text" name="bulk"   value="{{d.bulk||''}}" placeholder="" autocomplete="off" class="layui-input ">
                    <select name="bulk_unit" style="width: 55px; height: 38px; display: inline-block; position: absolute; right: 0px; top: 0px;">
                        <option value="ML" {{d.bulk_unit=='ML'?'selected':''}}>ML</option>
                        <option value="MAH" {{d.bulk_unit=='MAH'?'selected':''}}>MAH</option>
                    </select>
                </div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">品牌Logo</label>
                <div class="layui-inline">{{d.logo||''}}</div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">是否授权</label>
                <div class="layui-inline">{{d.authorization==1?'已授权':'未授权'}}</div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">描述等级</label>
                <div class="layui-inline">{{d.grade||''}}</div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">开发人员</label>
                <div class="layui-inline">{{d.developer||''}}</div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">仿品(品牌)</label>
                <div class="layui-inline">{{d.imitation||''}}</div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">盗图/实拍图</label>
                <div class="layui-inline">{{d.realpic||''}}</div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">调研链接</label>
                <div class="layui-inline"><a class="blue" href="{{d.research_links||''}}" target="_blank">点击跳转</a></div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">开发链接</label>
                <div class="layui-inline"><a class="blue" href="{{d.develop_link||''}}" target="_blank">点击跳转</a></div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">交接备注</label>
                <div class="layui-inline">{{d.remarks||''}}</div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh">审核备注</label>
                <div class="layui-inline">{{d.audit_remarks||''}}</div>
            </div>
            <div class="layui-item">
                <label class="layui-label layui-form-label-yh" style="width: 120px">销售数量/单位</label>
                <div class="layui-inline">{{d.quantity||''}}{{d.quantity_unit||''}}</div>
            </div>
        </div>
    </div>


    {{# }}}
</script>
<script>
    layui.use(['admin', 'table', 'form', 'toolJ', 'formSelects', 'laydate', 'laytpl','upload', 'element','layedit','ajaxCascader'], function () {
        var $ = layui.$
            , admin = layui.admin
            , view = layui.view
            , table = layui.table
            , laytpl = layui.laytpl
            , cascader = layui.ajaxCascader
            , upload = layui.upload
            , laydate = layui.laydate
            , formSelects = layui.formSelects
            , toolJ = layui.toolJ
            , element = layui.element
            , layedit = layui.layedit
            , form = layui.form;
        form.render(null);
        form.verify({
            mustradio: function (value, item) { //单选按钮必选
                var va = $(item).find("input[type='radio']:checked").val();
                if (typeof (va) == "undefined") {
                    return $(item).attr("lay-verify-msg");
                }
            }
            , mustcheck: function (value, item) { //复选框必选
                var va = $(item).find("input[type='checkbox']:checked").val();
                if (typeof (va) == "undefined") {
                    return $(item).attr("lay-verify-msg");
                }
            }
        });
        var id = layui.router().search.id;//当前id
        var type=1;//1为单个保存 2批量spu保存
        var title={};//分类模板
        var desc={};//描述模板
        var cateId=[];//已选择的速卖通分类id 数组

        var attrObj={};//描述 速卖通 标题 已存在的值
        var template_name=-1;//当前选择的标题模板 名称
        var _sku='';//当前sku
        var submitData={};//提交所有表单数据数据
        var submitOk=0;//=2 已通过必填校验
        var interval='';//定时器
        var titleValue='';//显示标题值
        var titleValueLength=0;
        var isNew=0;//1为新品
        table.render({
            elem: '#LAY-list-normal'
            , url: '/index/Datamanufacture/getSpuAttrBySku?id='+id
            , cellMinWidth: 80
            , cols: [[
                {field: 'sku', title: 'SKU'}
                , {field: 'name', title: '采购名称'}
                , {field: 'color', title: 'Color',edit: 'text'}
                , {field: 'type', title: 'Type',edit: 'text'}
                , {field: 'shape', title: 'Shape',edit: 'text'}
                , {field: 'size', title: 'Size',edit: 'text'}
                , {field: 'pattern', title: 'Pattern',edit: 'text'}
            ]]
            , page: false
            , limit: 10000
            , text: {none: '暂无相关数据!'}
        });
        element.on('tab(showInfo)', function () {
            var layid = $(this).attr('lay-id');
            $("#tabtype").val(layid);
            $('.search').click();
        });
        //标题模板
        form.on('submit(LAY-list-form-template-Submit)', function (data) {
            submitOk++;
        })
        //速卖通
        form.on('submit(LAY-list-form-att-Submit)', function (data) {
            submitOk++;
        })


        function titleValueFn(){
            titleValue='';
            titleValueLength=0;
            if($(".LAY-list-form-template").length>0){
                $(".LAY-list-form-template [name]").each(
                    function(i) {
                        if (i == 0 || $(this).val() === '' || $(this).attr('name')==='') {
                        } else {
                            if($(this).attr('type')=='checkbox'){
                                if($(this).prop("checked")){
                                    titleValueLength+=$(this).val().length;
                                    titleValue += $(this).val() + ' ';
                                }
                            }else{
                                titleValueLength+=$(this).val().length;
                                titleValue += $(this).val() + ' ';
                            }
                        }
                    }
                )
                if(titleValueLength>0){
                    $('#titleValueId').show();
                    $('#titleValueLengthId').show();
                }
                $('#titleValueId').text("预览标题: "+titleValue);
                $('#titleValueLengthId').text("字数: "+titleValueLength);
            }else{
                clearInterval(interval);//停止
            }
    }

        //描述 做总控制提交所有数据及校验必填
        form.on('submit(LAY-list-form-desc-Submit)', function (data) {
            var submitData={};
            submitData.desc={};
            for(var i in desc){
                submitData.desc[i]=layedit.getContent(desc[i]);
            }
            submitData.id=id;
            submitData.cateId=cascader.getChooseData('#cateSmt');
            submitData.type=type;
            submitData.template = form.val("LAY-list-form-template");
            submitData.smt = form.val("LAY-list-form-att");
            submitData.attr =table.cache['LAY-list-normal'];

            submitData.lwh =$('input[name=lwh]').val();
            submitData.balelwh =$('input[name=balelwh]').val();
            submitData.asweight =$('input[name=asweight]').val();
            submitData.bulk =$('input[name=bulk]').val();
            submitData.baleweight =$('input[name=baleweight]').val();
            submitData.bulk_unit =$('select[name=bulk_unit]').val();
            if(submitOk!=2){
                submitOk=0;
                toolJ.error('还有必填项未处理!');
                return false;
            }
            submitOk=0;
            if(Object.keys(submitData.smt).length==0){
                //toolJ.error('产品属性必须选择!');
                //return false;
            }
            if(Object.keys(submitData.template).length==0){
                toolJ.error('标题必须选择!');
                return false;
            }
            if(Object.keys(submitData.attr).length==0){
                toolJ.error('多属性找不到,请刷新页面!');
                return false;
            }
            //判断sku是否有重复多属性
            var checkAtrr={};
            for(var i in  submitData.attr){
                delete submitData.attr[i].name;
                var s=toolJ.cloneObj(submitData.attr[i]);
                var sku=submitData.attr[i]['sku'];
                delete s.LAY_TABLE_INDEX;
                delete s.sku;
                delete s.id;
                checkAtrr[sku]=JSON.stringify(s);
            }
            var msg='';
            for(var j in checkAtrr){
                for(var k in checkAtrr){
                    if(j!=k){
                        if(checkAtrr[k]==checkAtrr[j]){
                            msg+='SKU:'+j+'  SKU:'+k +'  多属性重复!';
                            break;
                        }
                    }
                }
                if(msg){
                    break;
                }
            }
            if(msg){
                toolJ.error(msg);
                return false;
            }
            submitData.template=JSON.stringify(submitData.template);
            var smtKeyValue={};
            var smtKeyValueEn={};

            for (var i in submitData.smt){
                var j=i.replace(/\[[0-9]*\]/,"");
                if(j.indexOf('_other')>=0){
                    smtKeyValue['other']=submitData.smt[i];
                }else{
                    if(cateListObjValueType[j]=='check_box'||cateListObjValueType[j]=='list_box'){
                        var temp=cateListObjValue[submitData.smt[i]];
                    }else{
                        var temp=submitData.smt[i];
                    }
                    if(smtKeyValue[cateListObjTemp[j]]){
                        smtKeyValue[cateListObjTemp[j]]+="卍"+temp;
                    }else{
                        smtKeyValue[cateListObjTemp[j]]=temp;
                    }

                    if(cateListObjValueType[j]=='check_box'||cateListObjValueType[j]=='list_box'){
                        var temp=cateListObjValueEn[submitData.smt[i]];
                    }else{
                        var temp=submitData.smt[i];
                    }
                    if(smtKeyValueEn[cateListObjKey[j]]){
                        smtKeyValueEn[cateListObjKey[j]]+="卍"+temp;
                    }else{
                        smtKeyValueEn[cateListObjKey[j]]=temp;
                    }

                }
            }
            submitData.smtKeyValue=smtKeyValue;
            submitData.smtKeyValueEn=smtKeyValueEn;
            submitData.smt=JSON.stringify(submitData.smt);
            toolJ.ajax({"data": JSON.stringify(submitData), 'url': '/index/Datamanufacture/manufactureEdit','contentType':'application/json'});
        })
        //单个保存 type=1;
        $('.oneUpdate').on('click',function () {
            type=1;
            save();
        })
        //批量保存 type=2;
        $('.batchUpdate').on('click',function () {
            type=2;
            save();
        })
        //保存提交数据
        function save(){
            if(isNew){
                if(titleValueLength<100){
                    toolJ.error('标题字数不能少于100');
                    return false;
                }
            }

            //同步描述到textarea 触发校验是否必填

            for(var i in desc){
                layedit.sync(desc[i]);
            }
            submitOk=0;
            $("[lay-filter=LAY-list-form-template-Submit]").click();
            $("[lay-filter=LAY-list-form-att-Submit]").click();
            $("[lay-filter=LAY-list-form-desc-Submit]").click();
            return false;
        }

        //解析标题模板
        form.on('select(template_name)', function (data) {
            if(template_name==data.value){
                return false;
            }
            template_name=data.value;
            titleTemplate(template_name);
        });

        //标题模板
        function titleTemplate(name){
            var html='';
            for (var i in title){
                if(i==name){
                    var t=title[i];
                    for (var j in t){
                        var d=t[j];
                        if(d.field_type==1){
                            d.field_type='input';
                        }else if(d.field_type==2){
                            d.field_type='check_box';
                        }else if(d.field_type==3){
                            d.field_type='list_box';
                        }else if(d.field_type==4){
                            d.field_type='textarea';
                            text.push(d.field);
                        }
                        var attr=d.arguments.split(',');
                        var attrValues=[];
                        for (var k in attr){
                            attrValues.push({"valueZhName":attr[k],"valueId":attr[k]})
                        }
                        var v='';
                        if(attrObj.title_template.name==name){
                            var vs=attrObj.title_template.value;
                            for(var l in vs){
                                if(l==d.field){
                                    v=vs[l];
                                }
                            }
                        }
                        html+=analyzeAtt({
                            attributeShowTypeValue:d.field_type,
                            keyZhName: d.field,
                            attrValues:attrValues,
                            requiredFlag:d.required==1?true:false,
                            id:d.field
                        },v);
                    }
                }
            }
            $('.LAY-list-form-template-content').html(html);
            form.render(null,'LAY-list-form-template');
        }

        //加载标题分类
        toolJ.ajax({'url': '/index/Datamanufacture/getTemplate','data':{'id':id}}, function (res) {
            var selectHtml = '';
            title=res.data.title;
            _sku=res.data.skuInfo.sku;
            isNew=res.data.skuInfo.new;
            attrObj=res.data.attr;
            for (var i in res.data.title) {
                selectHtml += '<option value="' + i + '">' + i + '</option>';
            }
            $(".LAY-list-form-template select[name='name']").append(selectHtml);
            if(attrObj.title_template.name){
                $(".LAY-list-form-template select[name='name']").val(attrObj.title_template.name);
                titleTemplate(attrObj.title_template.name);
            }
            form.render(null,'LAY-list-form-template');
            //加载sku信息
            var getTpl = skuInfo.innerHTML;
            laytpl(getTpl).render(res.data.skuInfo, function (html) {
                $('.skuInfo').html(html);
            })
            //加载描述模板
            var text=[];
            var html='';
            for (var j in  res.data.desc){
                var d=res.data.desc[j];
                if(d.field_type==4){
                    d.field_type='textarea';
                    text.push(d.field);
                    var attr=d.arguments.split(',');
                    var attrValues=[];
                    for (var k in attr){
                        attrValues.push({"valueZhName":attr[k],"valueId":attr[k]})
                    }
                    html+=analyzeAtt({
                        attributeShowTypeValue:d.field_type,
                        keyZhName: d.field,
                        attrValues:attrValues,
                        requiredFlag:d.required==1?true:false,
                        id:d.field
                    });
                }
            }
            $('.desc').html(html);
            //把已保存的描述数据填充页面
            for(var i in text){
                var t=text[i].replace(/\s+/g,"___");
                desc[text[i]]=layedit.build(t) //建立编辑器
            }
            //1秒后加载已有描述,有延迟
            setTimeout(function () {
                for(var i in text){
                    var t=text[i].replace(/\s+/g,"___");
                    for (var j in res.data.attr['desc_template']){
                        var jv= res.data.attr['desc_template'][j];
                        if(j==text[i]){
                            $('iframe[textarea='+t+']').prop("contentWindow").document.body.innerHTML=jv;
                        }
                    }
                }
            },1000)
            getSmtCate(res.data.skuInfo.smt_cid);

        })

        var cateList={};

        //获取速卖通分类
        function getSmtCate(path){
            if(attrObj.smt_template.cateId){
                cateId=attrObj.smt_template.cateId.split('卍');
            }else{
                if(path&&path.indexOf('_')!=-1){
                    cateId=path.split('_');
                }else{
                    cateId=[];
                }
            }
            //获取速卖通分类
            toolJ.ajax({'url': '/index/Datamanufacture/getSmtCate'}, function (res) {
                // 直接赋值模式
                cascader.load({
                    elem: '#cateSmt',
                    width: 400, //input框宽度
                    // placeholder, // input框提示词
                    prop: {
                        value: 'cate_id',
                        label: 'cate_name_cn'
                        //children:'children' // 子集的名称定义
                    }
                    , clear: true                               // 是否显示清空功能
                    ,clicklast:true			      //【可选】点击最后一级才选择数据
                    , data: res.data
                    , chooseData: cateId // 回显数据
                });
                if(cateId.length>0){
                    getSmtCateAttributeByCateId(cateId[cateId.length-1]);
                }
                // 选择器点击事件的监听
                cascader.on('click', '#cateSmt', function(data){
                    if(data.cate_id){
                        getSmtCateAttributeByCateId(data.cate_id)
                    }
                });
            })
        }
        function isInArray(arr,value){
            if(arr && typeof arr==='object'){
                for(var i = 0; i < arr.length; i++){
                    if(value == arr[i]){
                        return true;
                    }
                }
            }
            return false;
        }
        var cateListObj={};

        var cateListObjTemp={};
        var cateListObjValue={};//数字值对应的中文值
        var cateListObjValueEn={};//value数字值对应的英文值

        var cateListObjKey={};//key数字对应的英文值

        var cateListObjValueType={};
        var cateListObjOld={};//兼容旧数据

        //获取速卖通分类属性
        function getSmtCateAttributeByCateId(cate_id) {
            toolJ.ajax({"data":{"cateId":cate_id},'url': '/index/Datamanufacture/getSmtCateAttributeByCateId'}, function (res) {
                cateList=[];
                cateListObj={};
                cateListObjTemp={};
                if(res.code==1){
                    for(var j in res.data){
                        var att=res.data[j];
                        att.id='s'+att.id;
                        //att.keyZhName=JSON.parse(att.names)['en'];
                        cateListObjKey[att.id]=JSON.parse(att.names)['en'];
                        cateListObjValueType[att.id]=att.attributeShowTypeValue;
                        for (var j in att.attrValues) {
                            //att.attrValues[j].valueZhName=JSON.parse(att.attrValues[j].names)['en'];
                            cateListObjValue[att.attrValues[j].valueId]=att.attrValues[j].valueZhName;
                            cateListObjValueEn[att.attrValues[j].valueId]=JSON.parse(att.attrValues[j].names)['en'];
                        }
                        cateList.push(att);
                        cateListObj[att.id]=att;
                        cateListObjOld[att.keyId]=att;
                        cateListObjTemp[att.id]= att.keyZhName;
                    }
                    var html='';
                    var old=0;
                    if(cateId.length>0){
                        if(cateId[cateId.length-1]==cate_id){
                            //加载已存在的速卖通属性值
                            var vs=attrObj.smt_template.value;
                            for(var l in vs){
                                if(l.indexOf('-')!=-1){
                                    var pid=l.split('-')[1];
                                    var ll=l.split('-')[0];
                                }else{
                                    var pid='';
                                    var ll=l;
                                }
                                if(l.indexOf('s')==-1){
                                    var att=cateListObjOld[ll];
                                    old=1;
                                }else{
                                    var att=cateListObj[ll];
                                }
                                //判断是否有子属性
                                if(l.indexOf('-')!=-1){
                                    if(!att.skuFlag&&att.keyId!=2){
                                        html+= analyzeAtt(att,vs[l],pid);
                                    }
                                }else if(l.indexOf('_other')!=-1){
                                    //其他类型 特殊属性
                                    html+=analyzeAtt({
                                        attributeShowTypeValue:'input',
                                        keyZhName: "",
                                        id:l
                                    },vs[l],l.split('_')[0]);
                                }
                                else{
                                    if(att&&!att.skuFlag&&att.parentId==0&&att.keyId!=2){
                                        html+= analyzeAtt(att,vs[l],pid);
                                    }
                                }
                                if(l.indexOf('s')==-1){
                                    delete  cateListObjOld[ll];
                                }else{
                                    delete  cateListObj[l];
                                }
                            }
                        }
                    }
                    if(old==1){
                        var o=cateListObjOld;
                    }else{
                        var o=cateListObj;
                    }
                    for(var i in o){
                        var att=o[i];
                        if(att&&!att.skuFlag&&att.parentId==0&&att.keyId!=2){
                            html+= analyzeAtt(att);
                        }
                    }
                    $('.LAY-list-form-att').html(html);
                    form.render(null, 'LAY-list-form-att');


                }
            })
        }

        //解析分类属性 转换成html
        function analyzeAtt(data,v,pid) {
            var html='';
            var requireed='';
            var str='';
            var verify='';
            if(data.requiredFlag){
                requireed='layui-form-label-required';
                verify='lay-verify="required"';
            }else{
                requireed='layui-form-label-yh';
            }
            if(!pid){
                pid='';
            }
            switch (data.attributeShowTypeValue) {
                case 'check_box':
                    //多选
                    if(v){
                        v=v.split('卍');
                    }
                    if(verify){
                        verify='lay-verify="mustcheck" lay-verify-msg="必选项不能为空" ';
                    }
                    for(var i in data.attrValues){
                        var checked='';
                        if(isInArray(v,data.attrValues[i].valueId)){
                            checked='checked';
                        }
                        str+='<input '+checked+' type="checkbox" name="'+data.id+'[]" value="'+data.attrValues[i].valueId+'" lay-skin="primary"  title="'+data.attrValues[i].valueZhName+'">';
                    }
                    html='                <div class="layui-form-item keyId'+ pid+'"  >\n' +
                        '                    <label class="layui-form-label '+requireed+'" >'+data.keyZhName+'</label>\n' +
                        '                    <div class="layui-input-block" '+verify+'>\n' +str+
                        '                    </div>\n' +
                        '                </div>';
                    break;
                case 'list_box':
                    //下拉单选
                    var leaf='';
                    for(var i in data.attrValues){
                        var selected='';
                        if(v==data.attrValues[i].valueId){
                            selected='selected';
                        }
                        str+='<option '+selected+' value="'+data.attrValues[i].valueId+'">'+data.attrValues[i].valueZhName+'</option>';
                        if(data.attrValues[i].leaf===false){
                            if(leaf){
                                leaf+=','+data.attrValues[i].valueId;
                            }else{
                                leaf=data.attrValues[i].valueId;
                            }
                        }
                    }
                    html='                <div class="layui-form-item keyId'+ pid+'" >\n' +
                        '                    <label class="layui-form-label '+requireed+'" >'+data.keyZhName+'</label>\n' +
                        '                    <div class="layui-input-block layui-jy-width200" >\n' +
                        '                        <select '+verify+' name="'+data.id+'" leafid="'+leaf+'" lay-filter="smtAttr">\n' +
                        '                            <option value="">请选择</option>\n' +str+
                        '                        </select>\n' +
                        '                    </div>\n' +
                        '                </div>';
                    break;
                case 'input':
                    //文本框
                    if(typeof v=="undefined"){
                        v='';
                    }
                    str+=' <input '+verify+' type="text" name="'+data.id+'" value="'+v+'"  autocomplete="off" placeholder="请输入" class="layui-input">';
                    html='                <div class="layui-form-item keyId'+ pid+'" >\n' +
                        '                    <label class="layui-form-label '+requireed+'" >'+data.keyZhName+'</label>\n' +
                        '                    <div class="layui-input-block" style="width: 83%">\n' +str+
                        '                    </div>\n' +
                        '                </div>';
                    break;
                case 'interval':
                    //范围
                    if(v){
                        v=v.split('卍');
                    }else{
                        v=[];
                        v[0]='';
                        v[1]='';
                    }
                    html+=' <div class="layui-form-item keyId'+ pid+'" >\n' +
                        '    <div class="layui-inline">\n' +
                        '      <label class="layui-form-label '+requireed+'">'+data.keyZhName+'</label>\n' +
                        '      <div class="layui-input-inline" style="width: 100px;">\n' +
                        '        <input '+verify+' type="text" name="'+data.id+'[]" placeholder="" value="'+v[0]+'" autocomplete="off" class="layui-input">\n' +
                        '      </div>\n' +
                        '      <div class="layui-form-mid">-</div>\n' +
                        '      <div class="layui-input-inline" style="width: 100px;">\n' +
                        '        <input '+verify+' type="text" name="'+data.id+'[]" placeholder="" value="'+v[1]+'" autocomplete="off" class="layui-input">\n' +
                        '      </div>\n' +
                        '    </div>\n' +
                        '  </div>';
                    break;
                case  'textarea':
                    data.id=data['id'].replace(/\s+/g,"___");//兼容不支持空格的情况
                    html+='                            <div class="layui-card-header '+requireed+'">\n' +
                        '                                '+data.keyZhName+'\n' +
                        '                            </div>\n' +
                        '                            <textarea '+verify+' id="'+data.id+'" style="display: none;"></textarea><br/>';
                    break;
            }
            return html;
        }
        //监听下拉显示子属性
        form.on('select(smtAttr)', function(data){
            var leafid=data.elem.getAttribute('leafid');

            //如有已存在子属性 先清空
            $(".keyId"+data.elem.name).remove();

            var html='';
            leafid=leafid.split(',');
            if(!data.value){
                return false;
            }
            if(isInArray(leafid,data.value)){
                //存在子属性
                for (var i in cateList){
                    var d=cateList[i];
                    if(d.parentId==data.value){
                        d.id=d.id+'-'+data.elem.name;
                        html=analyzeAtt(d,'',data.elem.name);
                    }
                }
                $(data.othis).parent().parent().after(html);
                form.render(null, 'LAY-list-form-att');
            }
            if(data.value==4){
                //存在其他属性 新增一个文本框
                html=analyzeAtt({
                    attributeShowTypeValue:'input',
                    keyZhName: "",
                    id:data.elem.name+'_other'
                },'',data.elem.name);
                $(data.othis).parent().parent().after(html);
                form.render(null, 'LAY-list-form-att');
            }
        });

        interval =setInterval(titleValueFn,1500);

    });
</script>